import type { VbenFormProps } from '@vben-core/form-ui';

import type { TemplateFormConfig } from '#/views/onlineDesign/designPage/template/type/type';

import { querySelectTableApi } from '#/api';
import { queryDictOptionsApi } from '#/utils';
import { tableConfig } from '#/views/onlineDesign/designPage/template/tableTemplate/config';
import { formFun } from '#/views/onlineDesign/designPage/template/utils/formUtils';
import {
  selectFormFun,
  toCamelCase,
} from '#/views/onlineDesign/designPage/template/utils/selectFormUtils';

/**
 * 生成查询表单，目前只有输入框和下拉框
 * @param fields 字段信息
 */
export const generateSelectForm = async (fields: Array): VbenFormProps => {
  return fields
    .filter(({ isHandle, fieldSelect }) => isHandle && fieldSelect === 1)
    .map((field) => {
      // 转为驼峰
      const camelCaseLabel = toCamelCase(field.fieldName);
      // 公共配置
      const commonProps = {
        label: field.fieldAnnotation,
        fieldName: camelCaseLabel,
        otherConfig: {
          showSelect: true,
          fieldRelevanceMode: field.fieldRelevanceMode,
          fieldPackagePath: field.fieldPackagePath,
        },
      } as TemplateFormConfig.FormComponentConfig;
      return selectFormFun.getComponent(field, commonProps);
    });
};

/**
 * 重新生成查询表单
 * 调用接口的前提是必须调用：config.updateTableConfig() 方法进行赋值
 */
export const generateLoadSelectForm = async (): VbenFormProps => {
  // 处理特殊组件渲染不出来的情况
  return tableConfig.selectFormConfig.formOption.map((item) => {
    // 如果 item.component 是 "ApiSelect"，则给 componentProps 添加 api 属性
    if (item.component === 'ApiSelect') {
      // eslint-disable-next-line unicorn/prefer-ternary
      if ('dictCode' in item.componentProps.params) {
        // 字典
        return {
          ...item,
          componentProps: {
            ...item.componentProps,
            // 添加或覆盖 api 属性
            afterFetch: (data) =>
              data.map(({ itemText, itemVal }) => ({
                label: itemText,
                value: item.otherConfig.fieldPackagePath.includes('Integer')
                  ? Number.parseInt(itemVal)
                  : itemVal,
              })),
            api: queryDictOptionsApi,
          },
        };
      } else {
        // 表下拉
        return {
          ...item,
          componentProps: {
            ...item.componentProps,
            // 添加或覆盖 api 属性
            api: querySelectTableApi,
          },
        };
      }
    } else {
      // 如果不是 "ApiSelect"，直接返回原对象
      return item;
    }
  });
};

/**
 * 生成表单
 * @param fields 字段信息
 */
export const generateForm = (fields: Array): VbenFormProps => {
  return fields
    .filter(({ isHandle }) => isHandle)
    .map((field, index) => {
      // 转为驼峰
      const camelCaseLabel = toCamelCase(field.fieldName);
      // 公共配置
      const commonProps = {
        label: field.fieldAnnotation,
        fieldName: camelCaseLabel,
        otherConfig: {
          fieldRelevanceMode: field.fieldRelevanceMode,
          fieldPackagePath: field.fieldPackagePath,
        },
        sequence: index + 1,
      } as TemplateFormConfig.FormComponentConfig;
      return formFun.getComponent(field, commonProps);
    });
};
